using System;
using System.Data;
using Sharepoint.ImportExport.Extractor;
using SharePoint.ImportExport.Core;


namespace cmdSharePointExtractor
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    public class cmdUI
    {
        static string version = "1.0";

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Parameters parameters = new Parameters();
            if (args.Length < 6)
            {
                PrintUsage();
                return;
            }

            if (!parameters.ReadParameters(args))
            {
                PrintUsage();
                return;
            }

            Console.WriteLine("*******************************************************");
            Console.WriteLine("You are perfoming an export with the following options");
            Console.WriteLine("\t Export is performed from: " + parameters.SiteURL);
            Console.WriteLine("\t Data file is written to: " + parameters.OutputDirectory);
            if (parameters.ExcludedLists.Length>0)
            {
                Console.WriteLine("\t The following lists/libraries are excluded");
            }
            if (parameters.List.Length>0)
            {
                Console.WriteLine("\t Only the following lists/libraries is exported {0}", parameters.List);
            }

            Console.WriteLine();

            Console.WriteLine("*******************************************************");


            Logger.Init("export" + DateTime.Now.ToString("MMddHHmmss") + ".log", ErrorLevel.Information);
            Logger.LogDataSendingEvent += new Logger.LogDataEventHandler(IncomingEventLogData);

            Sharepoint.ImportExport.Extractor.Exporter extractor = new Exporter();

            DataSet ds = new DataSet("Export Parameters");
            DataTable dt = ds.Tables.Add("Parameters");

            dt.Columns.Add("SiteURL");
            dt.Columns.Add("OutputDirectory");
            dt.Columns.Add("SiteName");
            dt.Columns.Add("List");
            dt.Columns.Add("Owner");
            dt.Columns.Add("Contact");
            dt.Columns.Add("IncludeSubWebs", typeof(bool));
            dt.Columns.Add("ManagedPath");
            dt.Columns.Add("SiteDef");
            dt.Columns.Add("Folder");
            dt.Columns.Add("ExportItemsNewerThan", typeof(System.DateTime));
            dt.Columns.Add("ExportItemsOlderThan", typeof(System.DateTime));
            dt.Columns.Add("ExportDesign", typeof(bool));
            dt.Columns.Add("ExportUsers", typeof(bool));
            dt.Columns.Add("ExportVersions", typeof(bool));
            dt.Columns.Add("ExportDoclibsOnly", typeof(bool));
            dt.Columns.Add("ProcessGalleries", typeof(bool));
            dt.Columns.Add("ExportViews", typeof(bool));
            dt.Columns.Add("Compress", typeof(bool));
            dt.Columns.Add("CAML", typeof(string));
            dt.Columns.Add("Excludedlists", typeof(string));

            DataRow newRow = dt.NewRow();

            // Set values in the columns:
            newRow["SiteURL"] = parameters.SiteURL;
            newRow["OutputDirectory"] = parameters.OutputDirectory;
            newRow["SiteName"] = parameters.NewName;
            newRow["List"] = parameters.List;
            newRow["Owner"] = parameters.Owner;
            newRow["Contact"] = parameters.Contact;
            newRow["IncludeSubWebs"] = parameters.IncludeSubWebs;
            newRow["ManagedPath"] = parameters.ManagedPath;
            newRow["Folder"] = parameters.Folder;
            newRow["ExportItemsNewerThan"] = parameters.ExportItemsNewerThan;
            newRow["ExportDesign"] = parameters.ExtractDesign;
            newRow["ExportUsers"] = parameters.ExportUserInfo;
            newRow["ExportItemsOlderThan"] = parameters.ExportItemsOlderThan;
            newRow["ExportVersions"] = parameters.ExportVersions;
            newRow["ExportDoclibsOnly"] = false;
            newRow["ProcessGalleries"] = parameters.ProcessGalleries;
            newRow["ExportViews"] = parameters.ExportViews ;
            newRow["Compress"] = parameters.CompressData;
            newRow["ExcludedLists"] = parameters.ExcludedLists;
            newRow["SiteDef"] = parameters.SiteDef;

            
            Logger.Log(ErrorLevel.File, "SiteURL="+parameters.SiteURL);
            Logger.Log(ErrorLevel.File, "OutputDirectory="+parameters.OutputDirectory);
            Logger.Log(ErrorLevel.File, "SiteName="+parameters.NewName);
            Logger.Log(ErrorLevel.File, "List="+parameters.List);
            Logger.Log(ErrorLevel.File, "Owner="+parameters.Owner);
            Logger.Log(ErrorLevel.File, "Contact="+parameters.Contact);
            Logger.Log(ErrorLevel.File, "IncludeSubWebs="+parameters.IncludeSubWebs);
            Logger.Log(ErrorLevel.File, "ManagedPath="+parameters.ManagedPath);
            Logger.Log(ErrorLevel.File, "Folder="+parameters.Folder);
            Logger.Log(ErrorLevel.File, "ExportItemsNewerThan="+parameters.ExportItemsNewerThan);
            Logger.Log(ErrorLevel.File, "ExportDesign="+parameters.ExtractDesign);
            Logger.Log(ErrorLevel.File, "ExportUsers="+parameters.ExportUserInfo);
            Logger.Log(ErrorLevel.File, "ExportItemsOlderThan="+parameters.ExportItemsOlderThan);
            Logger.Log(ErrorLevel.File, "ExportVersions="+parameters.ExportVersions);
            Logger.Log(ErrorLevel.File, "ExportDoclibsOnly="+false.ToString());
            Logger.Log(ErrorLevel.File, "ProcessGalleries="+parameters.ProcessGalleries);
            Logger.Log(ErrorLevel.File, "ExportViews="+parameters.ExportViews );
            Logger.Log(ErrorLevel.File, "Compress="+parameters.CompressData);
            Logger.Log(ErrorLevel.File, "ExcludedLists="+parameters.ExcludedLists);
            Logger.Log(ErrorLevel.File, "SiteDef="+parameters.SiteDef);

            

            // Add the row to the rows collection.
            dt.Rows.Add(newRow);
            
            try
            {

                extractor.Process(ds);
            }
            catch (Exception Ex)
            {
                Console.WriteLine("Exception occurred :" + Ex.Message);
            }

            Logger.LogDataSendingEvent -= new Logger.LogDataEventHandler(IncomingEventLogData);
            Logger.Close();
        }

        private static void IncomingEventLogData(object sender, SPEventArgs e)
        {
            string message = e.Message;
            message = message.Replace("Information-", "Information\t");
            message = message.Replace("Warning-", "Warning\t");
            message = message.Replace("Error-", "Error\t\t");
            message = message.Replace("Progress-", "Progress\t");
            string logArgs = DateTime.Now.ToLongTimeString() + "  " + message;
            Console.WriteLine(logArgs);
        }


        static void PrintUsage()
        {
            Console.WriteLine("SharePoint data extractor Version {0}", version);
            Console.WriteLine();
            Console.WriteLine("---");
            Console.WriteLine("Usage:");
            Console.WriteLine("-url URL to the site/area to be extracted");
            Console.WriteLine("-name site name of the new site");
            Console.WriteLine("-out x:\\outputfolder");
            Console.WriteLine("-owner DOMAIN\\User");
            Console.WriteLine();
            Console.WriteLine("Optional:");
            Console.WriteLine("-managedpath [Sites|Teams]");
            Console.WriteLine("-sitedefinition Name of the site definition to be used when importing");
            Console.WriteLine("-list Name of list/library to be extracted");
            Console.WriteLine("-folder Name of folder to be extracted");
            Console.WriteLine("-includesubsites true/false");
            Console.WriteLine("-exportuserinfo true/false");
            Console.WriteLine("-exportdesign true/false");
            Console.WriteLine("-exportdoclibsonly true/false");
            Console.WriteLine("-exportversions true/false");
            Console.WriteLine("-exportviews true/false");
            Console.WriteLine("-excludedlists \"semicolon separatd list of lists/libraries to exclude");
            Console.WriteLine("-processgalleries true/false");
            Console.WriteLine("-contact user@domain.com");
            Console.WriteLine("-datethreshold DateTime threshold, if specified then items newer than the threshold will be migrated");
            Console.WriteLine("-exportitemsolderthan DateTime threshold, if specified then items older than the threshold will be migrated");
            //			Console.WriteLine("-reportLevel Information|Warning|Error");
            Console.WriteLine("-compress true/false");

            Console.WriteLine();
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
        }
    }
}